package studentStatus.view;

import studentStatus.constants.Sqls;
import studentStatus.tools.DatasourceUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class MessageWindow extends JFrame implements Sqls {

    public static MessageWindow instance;
    private JTextField nameField, phoneField, contentField;

    public static MessageWindow getInstance(){
        if (instance == null) {
            synchronized (MessageWindow.class) {
                if (instance == null) {
                    instance= new MessageWindow();
                }
            }
        }
        return instance;
    }

    private MessageWindow() {
        // 设置窗口标题
        setTitle("留言窗口");

        // 创建文本框
        nameField = new JTextField(20);
        phoneField = new JTextField(20);
        contentField = new JTextField(20);

        // 创建按钮
        JButton submitButton = new JButton("提交");
        JButton messageList = new JButton("留言列表");
        JButton cancelButton = new JButton("取消");

        // 设置布局管理器
        setLayout(new FlowLayout());

        // 将组件添加到窗口
        add(new JLabel("姓名:"));
        add(nameField);
        add(new JLabel("电话:"));
        add(phoneField);
        add(new JLabel("内容:"));
        add(contentField);
        add(submitButton);
        add(messageList);
        add(cancelButton);

        messageList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TableWindow instance = TableWindow.getInstance();
                instance.setVisible(true);

            }
        });

        // 添加提交按钮的点击事件处理
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                String phone = phoneField.getText();
                String content = contentField.getText();
                if ("".equals(name) || "".equals(phone) || "".equals(content)) {
                    JOptionPane.showMessageDialog(null,"提交信息不能为空！");
                } else {
                    try {
                        DatasourceUtils.Update(sql5_add_message, new Object[]{
                                name, content, phone
                        });
                        dispose();
                        JOptionPane.showMessageDialog(null,"留言成功！");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        // 添加取消按钮的点击事件处理
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 在这里执行取消操作，可以清空文本框或关闭窗口
                nameField.setText("");
                phoneField.setText("");
                contentField.setText("");
                dispose();
            }
        });

        // 设置窗口大小和可见性
        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void centerWindow() {
        // 获取屏幕的尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        // 获取窗口的尺寸
        Dimension windowSize = getSize();

        // 计算窗口在屏幕上的位置
        int x = (screenSize.width - windowSize.width) / 2;
        int y = (screenSize.height - windowSize.height) / 2;

        // 设置窗口的位置
        setLocation(x, y);
    }
}
